#include <stdio.h>
#include <unistd.h>
#include <signal.h>

int child_pid;
int waitTime;

// Declaration of signal handlers
void increaseWaitTime(int);
void decreaseWaitTime(int);

int main()
{
	if( (child_pid = fork()) == 0 )
	{
		printf("Dies ist ein Kind-Prozess.\n");
		while(1)
		{
			printf("Child\n");
			sleep(1);
			kill(getppid(), SIGUSR1);
		}
	}
	else if( child_pid > 0 )
	{
		// Set up signal handlers
		signal(SIGUSR1, increaseWaitTime);
		signal(SIGUSR2, decreaseWaitTime);
		
		waitTime = 1000;
		printf("WaitTime: %d\n", waitTime);
		printf("Kind-Prozess %d wurde erzeugt.\n", child_pid);
		while(1)
		{
			printf("Parent\n");
			printf("WaitTime: %d\n", waitTime);
			sleep(5);
		}
	}
	else
	{
		printf("fork() failed");
	}
	return 0;
}

// Signal handler for SIGUSR1
void increaseWaitTime(int signum)
{
  waitTime = waitTime + 100;
}

// Signal handler for SIGUSR2
void decreaseWaitTime(int signum)
{
  if(waitTime > 0)
    waitTime = waitTime -100;
}
